/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.flow.order;

import cn.hutool.core.util.ObjectUtil;
import com.lxk.oms.cart.domain.vo.OmsCartVo;
import com.lxk.oms.cart.service.IOmsCartService;
import com.lxk.oms.order.domain.req.CreateOrderReq;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import org.dromara.common.core.utils.StreamUtils;

import java.util.List;

/**
 * 删除购物车信息
 *
 * @author heyi
 * 2024/9/23
 */
@LiteflowComponent(value = FlowOrderNodeConst.CART_DELETE)
@AllArgsConstructor
public class OrderRemoveCartNode extends NodeComponent {
    private final IOmsCartService cartService;

    @Override
    public void process() throws Exception {
        OrderContext contextBean = this.getContextBean(OrderContext.class);
        List<OmsCartVo> cartList = contextBean.getCartList();
        List<Long> idList = StreamUtils.toList(cartList, OmsCartVo::getId);
        cartService.deleteWithValidByIds(idList, false);
    }

    @Override
    public boolean isAccess() {
        OrderContext contextBean = this.getContextBean(OrderContext.class);
        return contextBean.getNeedStock() && ObjectUtil.equal(CreateOrderReq.CreateType.CART, contextBean.getCreateType()) && contextBean.getSaveDb();
    }
}
